package com.md.youyouhui.ui.shopping;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityShoppingPayBinding;
import com.md.youyouhui.net.response.GetShoppingDetailsResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.GlideApp;
import com.md.youyouhui.utils.GlideImageLoader;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/23.
 */
public class ShoppingPayActivity extends BaseActivity {
    private ActivityShoppingPayBinding payBinding;
    private String price, name, area, minute, code;
    private List<String> images = new ArrayList<>();
    private List<String> slideImg = new ArrayList<>();
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        payBinding = DataBindingUtil.setContentView(this, R.layout.activity_shopping_pay);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(payBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        int width = getWindowManager().getDefaultDisplay().getWidth();
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) payBinding.banner.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = width;
        payBinding.banner.setLayoutParams(layoutParams);
        String id = getIntent().getStringExtra("id");
        payBinding.payBtn.setOnClickListener(v -> {
            if (TextUtils.isEmpty(price)) {
                Toast.makeText(ct, "没有获取到商品价格", Toast.LENGTH_SHORT).show();
                return;
            }
            startActivity(new Intent(ct, ShoppingAddressActivity.class).putExtra("id", id).putExtra("price", price));
            finish();
        });
        getDetails(id);
    }

    private void getDetails(String id) {
        viewModel.getShoppingDetails(id).observe(this, getDetailsResponse -> {
            assert getDetailsResponse != null;
            switch (getDetailsResponse.status) {
                case LOADING:
                    payBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    payBinding.progress.setVisibility(View.GONE);
                    GetShoppingDetailsResponse data = getDetailsResponse.data;
                    if (null == data) {
                        return;
                    }
                    price = data.price;
                    payBinding.payName.setText(data.title);
                    payBinding.payPrice.setText(String.format("￥%s", data.price));
                    payBinding.payContent.setText(data.describe);
                    String[] img = data.img;
                    String[] slideshowImg = data.slideshowImg;
                    for (int i = 0; i < data.img.length; i++) {
                        images.add(img[i]);
                    }
                    if (null != slideshowImg) {
                        for (int i = 0; i < data.slideshowImg.length; i++) {
                            slideImg.add(slideshowImg[i]);
                        }
                    }
                    showViewpage();
                    showLongImage();
                    break;
                case ERROR:
                    payBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, getDetailsResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void showLongImage() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.weight = LinearLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        for (int i = 0; i < images.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(layoutParams);
            GlideApp.with(this).load(images.get(i)).placeholder(R.mipmap.app_icon).into(imageView);
            payBinding.payImage.addView(imageView);
        }
    }

    private void showViewpage() {
        /*homeBinding.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        homeBinding.banner.setIndicatorGravity(BannerConfig.CENTER);*/
        payBinding.banner.setDelayTime(3000);
        //设置图片源
        payBinding.banner.setImages(slideImg);
        payBinding.banner.setImageLoader(new GlideImageLoader());
        //设置标题源
        //homeBinding.banner.setBannerTitles(titles);
        payBinding.banner.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        payBinding.banner.stopAutoPlay();
    }
}
